package cn.droidlover.mywegit.util;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Weather  extends ViewModel {


    private MutableLiveData<String> city;
    private  MutableLiveData<String> country;
    private  MutableLiveData<String> update_time;

    List<WeatheData> D;

    public MutableLiveData<String> getCity() {

        if(city==null)
        {
            city=new MutableLiveData<>();
            city.postValue("上海");

        }
        return city;
    }

    public MutableLiveData<String> getCountry() {

        if(country==null)
        {
            country=new MutableLiveData<>();
            country.postValue("");
        }
        return country;
    }

    public MutableLiveData<String> getUpdate_time() {
        if (update_time==null)
        {
            update_time=new MutableLiveData<>();
            update_time.postValue("");

        }
        return update_time;
    }

   // List<WeatheData> data;

    List<MutableLiveData<WeatheData>> data;

    MutableLiveData<List<WeatheData>> data1;

    public MutableLiveData<List<WeatheData>> getData1() {
        if(data1==null)
        {
             D=new ArrayList<>();
            for(int i =0;i<7;i++){
                D.add(new WeatheData());
            }

            data1=new MutableLiveData<>();
            data1.postValue(D);


        }
        return data1;
    }

    public List<MutableLiveData<WeatheData>> getData() {

        if(data==null)
        {

            data=new ArrayList<MutableLiveData<WeatheData>>(7) ;

            for(int i=0;i<7;i++)
            {
                data.add(new MutableLiveData<WeatheData>());
            }
        }
        return data;
    }

    public Weather(){


        if(data==null)
        {

            data=new ArrayList<>(7) ;

            for(int i=0;i<7;i++)
            {
                data.add(new MutableLiveData<WeatheData>());
            }
        }


        if(country==null)
        {
            country=new MutableLiveData<>();
            country.postValue("");
        }

        if(city==null)
        {
            city=new MutableLiveData<>();
            city.postValue("上海");

        }

        if (update_time==null)
        {
            update_time=new MutableLiveData<>();
            update_time.postValue("");

        }


        if(data1==null)
        {
            D=new ArrayList<>();
            for(int i =0;i<7;i++){
                D.add(new WeatheData());
            }

            data1=new MutableLiveData<>();
            data1.postValue(D);


        }
    }


    public void setHttpResult(String msg)
    {
        Map<String, Object> jsMap = FastJsonUtils.changeJsonToMap(msg);

        this.city.postValue(jsMap.get("city").toString()); ;
        this.country.postValue(jsMap.get("country").toString());
        this.update_time.postValue(jsMap.get("update_time").toString());  ;

        String datas=jsMap.get("data").toString();
        JSONArray jsonArray = FastJsonUtils.jsonStringToArray(datas);
        int count=jsonArray.size();

       // Log.d("HTTPOK", "count:"+D.size());



        for (int i=0;i<count;i++)
        {
            //Log.d("HTTPOK", "count:"+i);



            if(data1!=null){

              D.get(i).setResult(jsonArray.get(i).toString());


            }





        }


       data1.postValue(D);

    }


    public void setJsonData(String msg)
    {

        Log.i("HTTPOK", msg);
        Map<String, Object> jsMap = FastJsonUtils.changeJsonToMap(msg);

        jsMap= FastJsonUtils.changeJsonToMap(jsMap.get("showapi_res_body").toString());

        //更新时间
        if(jsMap.containsKey("time")){

            update_time.postValue(jsMap.get("time").toString());
        }
        //地区
        if(jsMap.containsKey("cityInfo"))
        {
            String area=jsMap.get("cityInfo").toString();
            Map<String, Object> jsMap1 = FastJsonUtils.changeJsonToMap(area);
            this.city.postValue(jsMap1.get("c7").toString()+"."+jsMap1.get("c3").toString());
        }
        //第一天
        if(jsMap.containsKey("f1")){
               D.get(0).setResult(jsMap.get("f1").toString());
        }

        //第2天
        if(jsMap.containsKey("f2")){
            D.get(1).setResult(jsMap.get("f2").toString());
        }

        //第3天
        if(jsMap.containsKey("f3")){
            D.get(2).setResult(jsMap.get("f3").toString());
        }

        data1.postValue(D);

    }




}
